<?php

session_start();
require_once('connect.php');
require_once('includes/user.class.php');
require_once('includes/subjects.class.php');

$database = Database::getInstance();
$user = new User();
$subjects = new Subject();

if(!$user->is_leader()) header('Location: index.php');
require_once('partials/header.php'); //wczytaj header i menu
?>


    <div class="container">

        <div class="row">

            <div class="col-md-3">

                <?php require_once('partials/content-left-menu.php'); ?>

            </div>

            <div class="col-md-9">

                <h2>Lista przedmiotów</h2>
                
				<div class="subject-table">
					<div class="subject-table-header subject-table-row">
						<div class="subject-table-header-cell">Nazwa</div>
                        <div class="subject-table-header-cell">Dzień</div>
                        <div class="subject-table-header-cell">Godzina</div>
                        <div class="subject-table-header-cell">Ilość miejsc</div>
					</div>

                    <?php
                    $subjectData = $subjects -> get_subjects();
                    foreach($subjectData as $subject){
                        print '<div class="subject-table-row">';
                        print'<div class="subject-table-header-cell">'.$subject['name'].'</div>';
                        print'<div class="subject-table-header-cell">'.$subject['date'].'</div>';
                        print'<div class="subject-table-header-cell">'.$subject['time'].'</div>';
                        print'<div class="subject-table-header-cell">'.$subject['space'].'</div>';
                        print '</div>';
                    }
                    ?>
					
				</div>
                <?php
                if($user->is_leader()){
                    if(isset($_POST['save_subject'])){
                        if(strlen($_POST['name'])>0 && strlen($_POST['hour'])>0 && strlen($_POST['space'])>0)
                        {
                            if(is_numeric($_POST['space'])){
                                $time = explode(':', $_POST['hour']);
                                $hours = (int)$time[0];
                                $minutes = (int)$time[1];

                                if(is_numeric($hours) && $hours < 24 && $hours >= 0 && $minutes < 60 && $minutes >= 0){
                                    $name = $_POST['name'];
                                    $date = $_POST['day'];
                                    $time = $hours.':'.$minutes.':00';
                                    $space = $_POST['space'];

                                    if($subjects->check_subject($name,$date,$time,$space)){
                                        $add = $subjects->add_subject($name,$date,$time,$space);
                                        if($add > 0){
                                            header( 'Location:subjects.php');
                                        }else{
                                            $alert = array('Wystąpił błąd, nie dodano przedmiotu.', 'danger');
                                        }
                                    }else{
                                        $alert = array('Taki przedmiot już istnieje!', 'danger');
                                    }
                                }
                                else{
                                    $alert = array('Podana godzina posiada nieprawidłowy format!', 'danger');
                                }
                            }else{
                                $alert = array('Ilość miejsc musi być cyfrą!', 'danger');
                            }

                        }else{
                        $alert = array('Nie wypełniono wszystkich wymaganych pól!', 'danger');
                        }

                    $user->show_alert($alert);
                    }
                    ?>
                        <div class="add-news-subject-area">
                            <form action = "subjects.php" method = "post">
                                <input type="text" name="name" id="name" class="subject-input" placeholder="Nazwa przedmiotu">
                                <select name="day" id="day" class="subject-input">
                                    <option value="Poniedziałek">Poniedziałek</option>
                                    <option value="Wtorek">Wtorek</option>
                                    <option value="Środa">Środa</option>
                                    <option value="Czwartek">Czwartek</option>
                                    <option value="Piątek">Piątek</option>
                                    <option value="Sobota">Sobota</option>
                                    <option value="Niedziela">Niedziela</option>
                                </select>
                                <input type="text" name="hour" id="hour" class="subject-input" placeholder="gg:mm">
                                <input type="text" name="space" id="space" class="subject-input" placeholder="Ilość miejsc">

                                <input type="submit" name="save_subject" value="Zapisz przedmiot w bazie" class="subject-button">
                            </form>
                        </div>
                        <?php
                    }
                ?>

					<!--TYLKO DLA ADMINOW -->
            </div>

        </div>

    </div>

 
<?php
require_once('partials/footer.php');
?>
